package com.sumer.psychology.space.resource;

import com.sumer.psychology.common.application.ApplicationServiceRegistry;
import com.sumer.psychology.common.domain.model.ChangePasswordCommand;
import com.sumer.psychology.common.domain.model.ChangeUserProfileCommand;
import com.sumer.psychology.common.domain.model.User;
import com.sumer.psychology.common.resource.AbstractResource;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Collections;
import java.util.Map;

/**
 * 空间rest资源
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/12/9
 * Time: 15:39
 */
@Path("/v1/space/")
public class SpaceResource extends AbstractResource {
    //private static Logger logger = LoggerFactory.getLogger(SpaceResource.class);

    @Context
    private UriInfo uriInfo;

    @GET
    @Path("{ownerId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response message(@PathParam("ownerId") String ownerId) {
        String json = ApplicationServiceRegistry
                .spaceApplicationQueryService()
                .findById(ownerId);
        return response(json);
    }

    @GET
    @Path("{ownerId}/friend_messages")
    @Produces(MediaType.APPLICATION_JSON)
    public Response friendMessage(@PathParam("ownerId") String ownerId) {
        String json = ApplicationServiceRegistry
                .spaceApplicationQueryService()
                .friendMessages(ownerId);
        return response(json);
    }

    @DELETE
    @Path("{ownerId}/friend_messages/{messageId}")
    public Response removeFriendMessage(
            @PathParam("ownerId") String ownerId,
            @PathParam("messageId") String messageId) {
        ApplicationServiceRegistry
                .spaceApplicationService()
                .removeFriendMessage(messageId, ownerId);

        return Response.accepted().build();
    }

    @GET
    @Path("{ownerId}/visitors")
    @Produces(MediaType.APPLICATION_JSON)
    public Response visitors(@PathParam("ownerId") String ownerId) {
        String json = ApplicationServiceRegistry
                .visitStatisticsRepository()
                .visitors(ownerId);
        return response(json);
    }

    @PUT
    @Path("{userId}/profile/photo")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response editPhoto(
            @PathParam("userId") String userId,
            Map<String, String> profile) {
        ApplicationServiceRegistry
                .profileRepository()
                .edit(userId, profile.get("photoId"));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    @GET
    @Path("{userId}/profile/photo")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getPhoto(@PathParam("userId") String userId) {
        String photoId = ApplicationServiceRegistry
                .profileRepository()
                .findPhoto(userId);

        if (photoId == null) {
            return Response.status(404).build();
        }

        return Response.ok(Collections.singletonMap("photoId", photoId)).build();
    }

    @GET
    @Path("{userId}/profile")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getProfile(@PathParam("userId") String userId) {
        User aUser = ApplicationServiceRegistry
                .userService()
                .userFrom(userId);

        if (aUser == null) {
            return Response.status(404).build();
        }

        return Response.ok(aUser).build();
    }

    @PUT
    @Path("{userId}/profile")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response editProfile(
            @PathParam("userId") String userId,
            ChangeUserProfileCommand aChangeUserProfileCommand) {
        ApplicationServiceRegistry
                .userService()
                .editUser(aChangeUserProfileCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }

    @PUT
    @Path("{userId}/password")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response changePassword(
            @PathParam("userId") String userId,
            ChangePasswordCommand aChangePasswordCommand) {
        ApplicationServiceRegistry
                .userService()
                .changePassword(aChangePasswordCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }
}
